package com.ict.ms.userLogin.service.api.impl;

import com.ict.ms.userLogin.service.dao.IUserInfoDao;
import com.ict.ms.userLogin.facade.model.UserInfo;
import org.springframework.stereotype.Service;
import com.ict.ms.userLogin.facade.api.IUserInfoService;
import javax.annotation.Resource;
import java.util.List;

@Service("userInfoService")
public class UserInfoService implements IUserInfoService {
    @Resource
    IUserInfoDao iUserInfoDao;

    public UserInfo getUserInfo(String userName,String passWord){
        try{
            UserInfo userInfo=iUserInfoDao.getUserInfo(userName);
            if(!userInfo.getPassWord().equals(passWord)){
                userInfo.setFlag(-1);
            }else{
                userInfo.setFlag(1);
            }
            return userInfo;
        }catch (Exception e){
            System.out.print("数据操作异常："+e);
            return null;
        }
    }

    public boolean userAdd(UserInfo userInfo){
        try{
            iUserInfoDao.userAdd(userInfo);
            return true;
        }catch (Exception e){
            System.out.print("数据操作异常："+e);
            return false;
        }
    }

    public boolean userDelete(String userName){
        try{
            iUserInfoDao.userDelete(userName);
            return true;
        }catch (Exception e){
            System.out.print("数据操作异常："+e);
            return false;
        }
    }

    public boolean userUpdate(UserInfo userInfo){
        try{
            iUserInfoDao.userUpdate(userInfo);
            return true;
        }catch (Exception e){
            System.out.print("数据操作异常："+e);
            return false;
        }
    }

    public List<UserInfo> getAllUser(){
        try{
            return iUserInfoDao.getAllUser();
        }catch (Exception e){
            System.out.print("数据操作异常："+e);
            return null;
        }
    }

    public int changePW(String userName,String pw,String newPW){
        UserInfo userInfo= iUserInfoDao.getUserInfo(userName);
        if(userInfo==null){
            //用户不存在
            return -1;
        }
        if(!userInfo.getPassWord().equals(pw)){
            //密码错误
            return -2;
        }else{
            userInfo.setPassWord(newPW);
            iUserInfoDao.userUpdate(userInfo);
            return 1;
        }
    }

}
